package com.maxatime.web.controller;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.maxatime.exception.PhoenirvanaException;
import com.maxatime.pojo.dto.ClassroomDTO;
import com.maxatime.service.ClassroomService;
import com.maxatime.util.MyContent;

@Controller
@RequestMapping("/classroom")
public class ClassRoomController {
//AJAX 
	// 日志
	Logger log = LogManager.getLogger();

	@Autowired
	ClassroomService classroomService;

	@RequestMapping("/mainpage")
	public String mainpage() {
		log.info("mainpage-begin");
		return "classroom/mainpage";
	}
	/**
	 * 
	 * @Title: queryData   
	 * @Description: TODO(教室管理系统)   
	 * @param: @param jsonStr
	 * @param: @return      
	 * @return: Map<String,Object>      
	 * @throws   
	 * @author: ssb    
	 * @date:   2023年10月30日 下午2:06:29   
	 * @version V1.0
	 */

	@RequestMapping("/queryData")
	@ResponseBody
	//查询
	public Map<String, Object> queryData(@RequestBody String jsonStr) {
		
		log.info("queryData-req::: " + jsonStr);//打印语句
		
		// 接收参数 1. 实体类， 2. json字符串
		
				// {"classroomNumber":"asdf","classroomName":"123"}
		JSONObject jsonObj = JSONObject.parseObject(jsonStr);
		String classroomNumber = jsonObj.getString("classroomNumber");
		String classroomName = jsonObj.getString("classroomName");
		
		Map<String, Object> m = new HashMap<String, Object>();
		log.info("begin-queryData");
		
		// service接口
		ClassroomDTO classroomDTO = new ClassroomDTO();//ClassroomDTO的对象
		classroomDTO.setClassroomNumber(classroomNumber);
		classroomDTO.setClassroomName(classroomName);
		List<ClassroomDTO> listDTO =  classroomService.listData(classroomDTO);	
		
		m.put(MyContent.CODE, "0000");
		m.put(MyContent.MSG, "交易成功");
		m.put(MyContent.DATA, listDTO);

		return m; 
	}
	@RequestMapping("/findByClassroomId")
	@ResponseBody
	public Map<String, Object> findByClassroomId(@RequestBody String jsonStr) {
		log.info("findByClassroomId-req::: " + jsonStr);
		// 接收参数 1. 实体类， 2. json字符串
		
		JSONObject jsonObj = JSONObject.parseObject(jsonStr);
		String classroomId = jsonObj.getString("classroomId");
		
		Map<String, Object> m = new HashMap<String, Object>();
		// service接口
		log.info("classroomService.findByClassroomId-req::: " + classroomId);
		ClassroomDTO classroomDTO = classroomService.findByClassroomId(classroomId);
		log.info("classroomService.listData-resp::: " + classroomDTO);
		
		m.put(MyContent.CODE, "0000");
		m.put(MyContent.MSG, "交易成功");
		m.put(MyContent.DATA, classroomDTO);

		return m;
		
	}
	
	@RequestMapping("/editData")
	@ResponseBody
	public Map<String, Object> editData(@RequestBody String jsonStr) {
		log.info("findByClassroomId-req::: " + jsonStr);
		
		//接受参数 1.实体类，2.json字符串
		JSONObject jsonObj = JSONObject.parseObject(jsonStr);
		String area = jsonObj.getString("area");
		String buildingNo = jsonObj.getString("buildingNo");
		String floor = jsonObj.getString("floor");
		String classroomNumber = jsonObj.getString("classroomNumber");
		String classroomName = jsonObj.getString("classroomName");
		String galleryful = jsonObj.getString("galleryful");
		String pkId = jsonObj.getString("pkId");
		
		Map<String, Object> m = new HashMap<String, Object>();
		// service接口
		ClassroomDTO classroomDTO = new ClassroomDTO();
		classroomDTO.setArea(area);
		classroomDTO.setBuildingNo(buildingNo);
		classroomDTO.setBuildingFloor(floor);
		classroomDTO.setClassroomNumber(classroomNumber);
		classroomDTO.setClassroomName(classroomName);
		classroomDTO.setGalleryful(Integer.parseInt(galleryful));
		classroomDTO.setPkId(Integer.parseInt(pkId));
		Date d=new Date();
		classroomDTO.setUpdateTime(d);
		log.info("classroomService.updateData-req::: " + classroomDTO);
		int iRet = classroomService.updateData(classroomDTO);	
		log.info("classroomService.updateData-resp::: " + iRet);
		
		m.put(MyContent.CODE, "0000");
		m.put(MyContent.MSG, "交易成功");
		m.put(MyContent.DATA, classroomDTO);
		return m;
		
	}
	//新增saveData
	@RequestMapping("/saveData")
	@ResponseBody
	public Map<String, Object> saveData(@RequestBody String jsonStr) {
		log.info("findByClassroomId-req::: " + jsonStr);
		
		//接受参数 1.实体类，2.json字符串
		JSONObject jsonObj = JSONObject.parseObject(jsonStr);//jsonStr字符串转成jsonObj对象
		String area = jsonObj.getString("area");
		String buildingNo = jsonObj.getString("buildingNo");
		String floor = jsonObj.getString("floor");
		String classroomNumber = jsonObj.getString("classroomNumber");
		String classroomName = jsonObj.getString("classroomName");
		String galleryful = jsonObj.getString("galleryful");
		String usestatus = jsonObj.getString("usestatus");
		
		Map<String, Object> m = new HashMap<String, Object>();
		// service接口
		ClassroomDTO classroomDTO = new ClassroomDTO();
		classroomDTO.setArea(area);
		classroomDTO.setBuildingNo(buildingNo);
		classroomDTO.setBuildingFloor(floor);
		classroomDTO.setClassroomNumber(classroomNumber);
		classroomDTO.setClassroomName(classroomName);
		classroomDTO.setGalleryful(Integer.parseInt(galleryful));
		classroomDTO.setUseStatus(usestatus);
		log.info("classroomService.updateData-req::: " + classroomDTO);
		int iRet = 0;
		
		try {
			iRet = classroomService.saveData(classroomDTO);
		} catch(PhoenirvanaException pe) {
			log.info("" + pe.getCode() + " msg:: " + pe.getMsg());
			m.put(MyContent.CODE, pe.getCode());
			m.put(MyContent.MSG, pe.getMsg());
			return m;
		}
		
		m.put(MyContent.CODE, "0000");
		m.put(MyContent.MSG, "交易成功");
		m.put(MyContent.DATA, classroomDTO);
		return m;
		
	}
	//删除deleteData
		@RequestMapping("/deleteData")
		@ResponseBody
		public Map<String, Object> deleteData(@RequestBody String jsonStr) {
			
			JSONObject jsonObj = JSONObject.parseObject(jsonStr);
			String pkId = jsonObj.getString("pkId");
			
			Map<String, Object> m = new HashMap<String, Object>();
			int iRet = classroomService.deleteByPrimaryKey(Integer.parseInt(pkId));
			
			m.put(MyContent.CODE, "0000");
			m.put(MyContent.MSG,"交易成功");
			return m;
			
		}
}
